package com.lb.shopguide.ui.fragment.guide.pay;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.CommitOrderReplyBean;
import com.lb.shopguide.event.guide.GoHomeEvent;
import com.lb.shopguide.event.guide.PaySuccessEvent;
import com.lb.shopguide.event.guide.QuickPayEvent;
import com.lb.shopguide.event.guide.RePayEvent;
import com.lb.shopguide.ui.activity.PayActivity;
import com.lb.shopguide.ui.fragment.guide.FragmentMain;
import com.lb.shopguide.ui.fragment.guide.order.FragmentOrderDetail;
import com.lb.shopguide.ui.fragment.guide.order.FragmentSureOrder;
import com.lb.shopguide.ui.view.NormalTitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kang on 2017/7/17.
 */

public class FragmentPayResult extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.iv_result)
    ImageView ivResult;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.tv_check_order)
    TextView tvCheckOrder;
    @BindView(R.id.tv_go_home)
    TextView tvGoHome;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    private boolean payFlag;
    private boolean quickPayFlag;
    private CommitOrderReplyBean mReplyBean;

    public static FragmentPayResult newInstance(boolean flag, CommitOrderReplyBean replyBean) {
        FragmentPayResult fragmentPayResult = new FragmentPayResult();
        Bundle bundle = new Bundle();
        bundle.putBoolean(AppConstant.PAY_RESULT, flag);
        bundle.putSerializable(AppConstant.COMMIT_REPLY_BEAN, replyBean);
        fragmentPayResult.setArguments(bundle);
        return fragmentPayResult;
    }

    public static FragmentPayResult newInstance(boolean flag, CommitOrderReplyBean replyBean, boolean quickPayFlag) {
        FragmentPayResult fragmentPayResult = new FragmentPayResult();
        Bundle bundle = new Bundle();
        bundle.putBoolean(AppConstant.PAY_RESULT, flag);
        bundle.putSerializable(AppConstant.COMMIT_REPLY_BEAN, replyBean);
        if (quickPayFlag) {
            bundle.putString(AppConstant.QUICK_PAY_MODE, AppConstant.QUICK_PAY_MODE);
        }
        fragmentPayResult.setArguments(bundle);
        return fragmentPayResult;
    }




    @Override
    public int getLayoutId() {
        return R.layout.fragment_pay_result;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.COMMIT_REPLY_BEAN)) {
            mReplyBean = (CommitOrderReplyBean) bundle.getSerializable(AppConstant.COMMIT_REPLY_BEAN);
        }
        if (bundle.containsKey(AppConstant.PAY_RESULT)) {
            payFlag = bundle.getBoolean(AppConstant.PAY_RESULT);
        }
        if (bundle.containsKey(AppConstant.QUICK_PAY_MODE)) {
            quickPayFlag = true;
        }
    }

    @Override
    public void initView() {
        ntb.setTitleText("收款结果");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishThisActivity();
            }
        });

        bindData();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentPayResult.class.getName();
    }

    private void bindData() {
        if (payFlag) {
            ivResult.setImageResource(R.drawable.iv_pay_success);
            tvResult.setText("交易成功");
            tvNextStep.setText("继续收银");
            EventBus.getDefault().post(new PaySuccessEvent());
        } else {
            tvCheckOrder.setVisibility(View.GONE);
            ivResult.setImageResource(R.drawable.iv_pay_error);
            tvResult.setText("交易失败");
            tvNextStep.setText("重新支付");
        }
        tvMoney.setText("+" + mReplyBean.getOrderTotalMoney());
    }

    @Override
    public boolean onBackPressedSupport() {
        finishThisActivity();
        return true;
    }
    @OnClick(R.id.tv_check_order)
    void checkOrder(){
        startWithPop(FragmentOrderDetail.newInstance(mReplyBean.getOrderNum(), AppConstant.FROM_PAY_ACTIVITY));
    }

    @OnClick(R.id.tv_go_home)
    void goHome(){
        if (_mActivity instanceof PayActivity) {
            _mActivity.finish();
        } else {
            pop();
        }
        EventBus.getDefault().post(new GoHomeEvent());
    }

    @OnClick(R.id.tv_next_step)
    void nextStep(){
        if (payFlag) {
            finishThisActivity();
        } else {
            EventBus.getDefault().post(new RePayEvent());
            pop();
        }
    }


    /**
     * 继续收银
     */
    private void finishThisActivity() {//为了快速收银做了修改
        if (_mActivity instanceof PayActivity) {
            if (((PayActivity) _mActivity).isQuickPayFlag() && payFlag) {
                EventBus.getDefault().postSticky(new QuickPayEvent());
            }
            _mActivity.finish();
        } else {
            if (quickPayFlag && payFlag) {
                popTo(FragmentMain.class, false, new Runnable() {
                    @Override
                    public void run() {
                        start(FragmentSureOrder.newInstance());
                    }
                });
            } else {
                pop();
            }
        }
    }
}
